import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import com.google.code.geocoder.Geocoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderAddressComponent;
import com.google.code.geocoder.model.GeocoderRequest;
import com.google.code.geocoder.model.GeocoderResult;
import com.google.code.geocoder.model.LatLng;


public class Geocoding {

	
	public static void main(String[] args) throws Exception{
		final Geocoder geocoder = new Geocoder();
		LatLng latLng = new LatLng(new BigDecimal(41.023625), new BigDecimal(29.224591));
		GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setLocation(latLng).getGeocoderRequest();
		GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
		//getCountry(geocoderResponse,"country");
		//getCountry(geocoderResponse,"locality");
		getData(geocoderResponse.getResults().get(0),"country");
		getData(geocoderResponse.getResults().get(0),"locality");
		getData(geocoderResponse.getResults().get(0),"neighborhood");
		getData(geocoderResponse.getResults().get(0),"administrative_area_level_2");
		System.out.println(geocoderResponse);
	}
	
	public static void getCountry(GeocodeResponse geocoderResponse,String type){
		List<GeocoderResult> list = geocoderResponse.getResults();
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			GeocoderResult geocoderResult = (GeocoderResult) iterator.next();
			if(geocoderResult.getTypes().contains(type))
				System.out.println(geocoderResult.getFormattedAddress());
		}
		
	}
	
	public static void getData(GeocoderResult geocoderResult,String type){
		List<GeocoderAddressComponent> list = geocoderResult.getAddressComponents();
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			GeocoderAddressComponent geocoderAddressComponent = (GeocoderAddressComponent) iterator.next();
			if(geocoderAddressComponent.getTypes().contains(type))
				System.out.println(geocoderAddressComponent.getLongName());
		}
	}
}
